package 动态规划;

public class _516_最长回文子序列 {
    public int longestPalindromeSubseq(String s) {
        if (s == null || s.length() == 0) return 0;
        int[][] dp = new int[s.length() + 1][s.length() + 1];

        for (int i = dp.length; i >= 0; i--) {
            dp[i][i] = 1;
            for (int j = i + 1; j < dp.length; j++) {
                if (s.charAt(i - 1) == s.charAt(j - 1)) {
                    dp[i][j] = dp[i + 1][j - 1] + 2;
                } else {
                    dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[1][s.length()];
    }
}
